package com.linghu.tankgame;

/**
 * @author 令狐荣豪
 * @version 1.0
 * 射击子弹
 */
public class Shot implements Runnable {
    int x;
    int y;
    int direct=0;
    int speed=2;
    boolean isLive=true;

    public Shot(int x, int y, int direct) {
        this.x = x;
        this.y = y;
        this.direct = direct;
    }

    @Override
    public void run() {//射击行为
        while (true){
            //子弹休眠
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //根据方向来改变x，y的坐标
            switch (direct){
                case 0://上
                    y-=speed;
                    break;
                case 1://右
                    x+=speed;
                    break;
                case 2://下
                    y+=speed;
                    break;
                case 3://左
                    x-=speed;
                    break;
            }

            //老师测试，输出我们的x和y
            System.out.println("子弹x="+x+"y="+y);
            //当子弹移动到面板边界时应该销毁，把启动的子弹线程销毁
            //当子弹碰到敌人坦克时，也应该结束线程
                    if (!(x>=0&&x<=1000&&y>=0&&y<=750&&isLive)){
                        isLive=false;
                        break;
                    }
            }
        }
    }

